package com.central.task.controller;

import com.central.common.model.ResourceInfo;
import com.central.task.service.IResourceManageService;
import com.central.common.annotation.LoginUser;
import com.central.common.annotation.RequiredPermission;
import com.central.common.constant.PermissionConstants;
import com.central.common.model.Result;
import com.central.common.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author Tindy
 * @date 2021/11/25
 * @describe
 */
@Slf4j
@RestController
@Api(tags = "资源管理模块api")
@RequestMapping(value = "/resourceManage")
public class ResourceManageController {

    @Autowired
    private IResourceManageService resourceManageService;


    @ApiOperation(value = "创建文件(夹)")
    @PostMapping("/create")
    @CacheEvict(value =  "resourcesTree",key = "#user.currentProjectId")
    @RequiredPermission({PermissionConstants.DEV_USER,PermissionConstants.PRO_ADMIN})
    public Result create(@ApiIgnore @LoginUser SysUser user, @RequestBody ResourceInfo resourceInfo){
        try {
            return resourceManageService.create(user,resourceInfo);
        }catch (Exception e){
            return Result.failed("操作失败："+e.getMessage());
        }
    }


    @ApiOperation(value = "删除文件（夹）")
    @DeleteMapping("/delete")
    @CacheEvict(value =  "resourcesTree",key = "#user.currentProjectId")
    @RequiredPermission({PermissionConstants.DEV_USER,PermissionConstants.PRO_ADMIN})
    public Result delete(@ApiIgnore @LoginUser SysUser user,Long id){
        try {
            return resourceManageService.delete(user,id);
        }catch (Exception e){
            return Result.failed("操作失败："+e.getMessage());
        }
    }

    @GetMapping("/resourcesTree")
    @ApiOperation(value = "查询所有资源目录树")
    @Cacheable(value = "resourcesTree",key = "#user.currentProjectId")
    public List<ResourceInfo> resourcesTree(@ApiIgnore @LoginUser SysUser user){
        List<ResourceInfo> resourceInfos = resourceManageService.findOnes(user.getCurrentProjectId(),0l);
        treeBuilder(resourceInfos);
        return resourceInfos;
    }
    /**
     * 递归循环实现建树
     *
     * @param resourceInfos
     * @return
     */
    public void treeBuilder(List<ResourceInfo> resourceInfos) {
        for (ResourceInfo resourceInfo : resourceInfos) {
            List<ResourceInfo> childs=resourceManageService.findChilds(resourceInfo.getId());
            treeBuilder(childs);
            resourceInfo.setChilds(childs);
        }
    }


    /**
     * 引用文件
     *
     * @param resourceId
     * @param msg
     * @return
     */
    @ApiOperation(value = "引用资源")
    @GetMapping("/quoteResource")
    public Long quoteResource(Long resourceId,String msg) {
        return resourceManageService.quoteResource(resourceId,msg);
    }

    /**
     * 删除引用
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除引用")
    @DeleteMapping("/delQuotes")
    public void delQuote(List<Long> ids) {
        resourceManageService.delQuotes(ids);
    }

    @ApiOperation(value = "通过ids查询资源文件")
    @PostMapping("/findResByIds")
    public List<ResourceInfo> findResByIds(@RequestBody List<Long> ids){
        return resourceManageService.findResByIds(ids);
    }
}
